package com.belf.console.agent.migrate;

import belf.migrate.engine.admin.taskconf.ConnectionConf;
import com.belf.console.domain.DataSourceInfo;

import static com.belf.common.constant.BelfCodeConstants.BELF_DATA_AUTOCOMMIT_TRUE;
import static com.belf.common.constant.BelfCodeConstants.BELF_DATA_USE_UNICODE_TRUE;

/**
 * BuildConnect
 *
 * @author l
 * @since 2024/7/19 14:31
 */

public class BuildConnect {

    /**
     * 构建连接配置
     *
     * @param sourceInfo 数据源信息
     * @return 连接配置
     */
    public static ConnectionConf connectionConf(DataSourceInfo sourceInfo) {
        ConnectionConf connectionConf = new ConnectionConf();
        connectionConf.setHost(sourceInfo.getIp());
        connectionConf.setPort(sourceInfo.getPort());
        connectionConf.setUser(sourceInfo.getUname());
        connectionConf.setPassword(sourceInfo.getPwd());
        connectionConf.setDriver(sourceInfo.getDriver());
        connectionConf.setUrl(sourceInfo.getUrl());
        connectionConf.setEncoding(sourceInfo.getEncoding());
        if (sourceInfo.getUseUnicode() != null) {
            connectionConf.setUseUnicode(sourceInfo.getUseUnicode().equals(BELF_DATA_USE_UNICODE_TRUE));
        }
        if (sourceInfo.getAutoCommit() != null) {
            connectionConf.setAutoCommit(sourceInfo.getAutoCommit().equals(BELF_DATA_AUTOCOMMIT_TRUE));
        }
        if (null != sourceInfo.getConnectTimeout()){
            connectionConf.setConnectTimeout(sourceInfo.getConnectTimeout());
        }
        if (sourceInfo.getSocketTimeout() != null) {
            connectionConf.setSocketTimeout(sourceInfo.getSocketTimeout());
        }
        return connectionConf;
    }
}
